package com.gzhuxj201.service.Impl;

import com.gzhuxj201.entity.*;
import com.gzhuxj201.entity.Class;
import com.gzhuxj201.mapper.ClassMapper;
import com.gzhuxj201.mapper.CourseMapper;
import com.gzhuxj201.mapper.HomeWorkMapper;
import com.gzhuxj201.mapper.SCMapper;
import com.gzhuxj201.service.HomeWorkService;
import com.gzhuxj201.util.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HomeWorkServiceImpl implements HomeWorkService {
    @Autowired
    private HomeWorkMapper homeWorkMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private SCMapper scMapper;

    @Transactional
    @Override
    public PageBean selectByClassId(Integer page, Integer pageSize,String courseId,String name) {
        log.info("courseId: "+courseId);
        //获取学生信息
        Student student = (Student) UserHolder.getUser();
        Short classId = student.getClassId();
        //计算起始索引
        Integer startIdx = (page - 1) * pageSize;
        //获取学生所选的课程号
        List<String> cnos = scMapper.selectBySno(student.getSno());
//        System.err.println("list等于null: "+(cnos==null));
//        System.err.println("list没有数据: "+(cnos.isEmpty()));
        if(cnos.isEmpty() || cnos == null){
            //如果还没有选修课程
            return null;
        }
        List<HomeWork> homeWorkList = homeWorkMapper.selectByClassId(startIdx,pageSize,classId,courseId,name,cnos);

        List<HomeWorkDto> homeWorkDtoList = homeWorkList.stream().map((item) -> {

            HomeWorkDto homeWorkDto = new HomeWorkDto();
            BeanUtils.copyProperties(item, homeWorkDto);
            //根据courseId查询cname
            Course course = courseMapper.selectByCno(item.getCourseId());
            homeWorkDto.setCname(course.getCname());
            //根据classId查询className
      /*      Class aClass = classMapper.selectById(item.getClassId());
            homeWorkDto.setClassName(aClass.getName());*/
            return homeWorkDto;
        }).collect(Collectors.toList());
        PageBean pageBean = new PageBean(homeWorkDtoList.size(), homeWorkDtoList);
        return pageBean;
    }


    @Transactional
    @Override
    public List<HomeWorkDto> selectByTno(String tno,String courseId) {

        List<HomeWork> homeWorkList = homeWorkMapper.selectByTno(tno,courseId);
        List<HomeWorkDto> homeWorkDtoList = homeWorkList.stream().map((item) -> {

            HomeWorkDto homeWorkDto = new HomeWorkDto();
            BeanUtils.copyProperties(item, homeWorkDto);
            //根据courseId查询cname
            Course course = courseMapper.selectByCno(item.getCourseId());
            homeWorkDto.setCname(course.getCname());
    /*        //根据classId查询className
            Class aClass = classMapper.selectById(item.getClassId());
            homeWorkDto.setClassName(aClass.getName());*/
            return homeWorkDto;
        }).collect(Collectors.toList());
        return homeWorkDtoList;
    }

    @Override
    public int add(HomeWork homeWork) {
        //获取教师编号和姓名
        Teacher teacher = (Teacher) UserHolder.getUser();
        homeWork.setTno(teacher.getTno());
        homeWork.setTeacherName(teacher.getUsername());
        return homeWorkMapper.add(homeWork);
    }

    @Override
    public int deleteById(Integer id) {
        return homeWorkMapper.deleteById(id);
    }

    @Override
    public HomeWork selectById(Integer id) {
        return homeWorkMapper.selectById(id);
    }

    @Override
    public int update(HomeWork homeWork) {
        return homeWorkMapper.update(homeWork);
    }


}